package com.helu.lubike.viewmodel;

import android.content.Context;
import android.databinding.ObservableField;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.helu.lubike.utils.Utils;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by helu on 17-2-12.
 */

public class BTSearchingViewModel {
    private static final String MSG = "BTSearchingViewModel";
    private LineChart mChart;
    private static final int MAX_CHATNUM = 20;
    private ArrayList<Integer> mRssiss = new ArrayList<>();
    private ArrayList<Integer> computeDistanceContainer = new ArrayList<>();
    private ArrayList<ILineDataSet> mDataSets;
    private static final long period = 2000;
    public final ObservableField<String> distance = new ObservableField<>();

    private Timer mTimer;


    public BTSearchingViewModel() {
        mTimer = new Timer();

        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                handleData();
            }
        };
        distance.set("0");
        mTimer.schedule(task, 0, period);//2秒后执行TimeTask的run方法
    }


    /**
     * 处理数据
     */
    private void handleData() {
        int sum = 0;
        for (int i = 0; i < computeDistanceContainer.size(); i++) {
            sum += computeDistanceContainer.get(i);
        }

        float ave;
        if (computeDistanceContainer.size() > 0) {
            ave = (sum * 1f) / computeDistanceContainer.size();
        } else {
            ave = 0;
        }
        String distancestr = Utils.calcDistanceForRSSI(ave);
        distance.set(distancestr);
    }


    /**
     * 添加RSSI信号强度
     *
     * @param rssi
     */
    public void addRssi(int rssi) {
        mRssiss.add(rssi);
        computeDistanceContainer.add(rssi);
        updateChart();
    }

    private void updateChart() {
        if (mChart.getLineData() != null) {
            mChart.getLineData().removeDataSet(0);

            if (mRssiss.size() > MAX_CHATNUM) {
                mRssiss.remove(0);
            }

            ArrayList<Entry> values = new ArrayList<>();
            for (int i = 0; i < mRssiss.size(); i++) {
                // y轴数据集
                values.add(new Entry(i, mRssiss.get(i)));
            }

            LineDataSet lineDataSet = new LineDataSet(values, "RSSI实时强度");
            lineDataSet.setDrawFilled(true);
            lineDataSet.setDrawCircles(false);
            lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
            lineDataSet.setDrawValues(false);
            mChart.getLineData().addDataSet(lineDataSet);
        } else {
            // y轴数据集
            ArrayList<Entry> values = new ArrayList<>();
            values.add(new Entry(0, mRssiss.get(0)));

            LineDataSet lineDataSet = new LineDataSet(values, "RSSI实时强度");
            lineDataSet.setDrawFilled(true);
            mDataSets = new ArrayList<>();
            mDataSets.add(lineDataSet); // add the datasets
            // create a data object with the datasets
            LineData data = new LineData(mDataSets);
            lineDataSet.setDrawFilled(true);
            lineDataSet.setDrawCircles(false);
            lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
            lineDataSet.setDrawValues(false);
            mChart.setData(data);
        }

        mChart.notifyDataSetChanged();
        // 将坐标移动到最新
        // 此代码将刷新图表的绘图
        mChart.moveViewToX(mChart.getLineData().getEntryCount());
    }

    public void initChart(LineChart lineChart) {
        mChart = lineChart;
        // enable touch gestures
        mChart.setTouchEnabled(false);
        // no description text
        mChart.getDescription().setEnabled(false);
        mChart.getXAxis().setEnabled(false);
        mChart.setNoDataText("暂时尚无数据");
    }

    public void destroy() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }


}
